package com.tencreat.mall.controller.admin;

import com.baomidou.lock.annotation.Lock4j;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.domain.mall.MallPaySetting;
import com.tencreat.service.mall.IMallPaySettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付设置Controller
 *
 * @Author tencreat
 * @Company: 深圳腾创互联科技有限公司
 * @link: https://tencreat.com
 * @Date 2022-07-07
 */
@Api(tags="支付设置api")
@RestController
@RequestMapping("/mall/pay-setting")
public class AdminMallPaySettingController extends BaseController {
    @Autowired
    private IMallPaySettingService mallPaySettingService;

    /**
     * 查询支付设置列表
     */
    @ApiOperation(value="查询支付设置列表")
    @GetMapping("/list")
    public HttpResult<List<MallPaySetting>> list(Integer type) {
        return HttpResult.ok(mallPaySettingService.lambdaQuery().eq(MallPaySetting::getType, type).orderByAsc(MallPaySetting::getSort).list());
    }

    @ApiOperation(value="修改支付设置")
    @Log(title = "基础管理>支付设置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Void> edit(@RequestBody MallPaySetting mallPaySetting) {
        mallPaySettingService.lambdaUpdate().set(MallPaySetting::getOpen, mallPaySetting.getOpen()).eq(MallPaySetting::getId, mallPaySetting.getId()).update();
        return HttpResult.ok();
    }

    @ApiOperation("修改排序")
    @GetMapping("/edit-sort")
    @Lock4j
    @Log(title = "基础管理>支付设置", businessType = BusinessType.UPDATE)
    HttpResult<Void> editSort(Long id, Integer type) {
        MallPaySetting mallPaySetting = mallPaySettingService.getById(id);
        MallPaySetting other;
        // 上移
        if (type == 0) {
            // 找到排序比自己小的
            other = mallPaySettingService.lambdaQuery()
                    .lt(MallPaySetting::getSort, mallPaySetting.getSort())
                    .eq(MallPaySetting::getType, mallPaySetting.getType())
                    .orderByDesc(MallPaySetting::getSort).last(" limit 1 ").one();
        }
        // 下移
        else {
            other = mallPaySettingService.lambdaQuery()
                    .gt(MallPaySetting::getSort, mallPaySetting.getSort())
                    .eq(MallPaySetting::getType, mallPaySetting.getType())
                    .orderByAsc(MallPaySetting::getSort).last(" limit 1 ").one();
        }
        if (other != null) {
            int sort = 999997;
            mallPaySettingService.lambdaUpdate().set(MallPaySetting::getSort, sort).eq(MallPaySetting::getId, mallPaySetting.getId()).update();
            mallPaySettingService.lambdaUpdate().set(MallPaySetting::getSort, mallPaySetting.getSort()).eq(MallPaySetting::getId, other.getId()).update();
            mallPaySettingService.lambdaUpdate().set(MallPaySetting::getSort, other.getSort()).eq(MallPaySetting::getId, mallPaySetting.getId()).update();
        }
        return HttpResult.ok();
    }

}
